package com.wxd.woff.table;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import com.wxd.woff.AbstractOTFDataStream;
import com.wxd.woff.AbstractWoffTable;
import com.wxd.woff.ByteBufferOTFDataStream;
import com.wxd.woff.WoffFont;

/**
 * cmap 表 Created with IntelliJ IDEA.
 *
 * @author: wangxindong
 * @date: 2018/8/30-21:40
 */
public class CmapTable extends AbstractWoffTable {

	/**
	 * platform id
	 */
	public static final int PLATFORM_UNICODE = 0;
	public static final int PLATFORM_MACINTOSH = 1;
	public static final int PLATFORM_WINDOWS = 3;

	/**
	 * Mac encodings
	 */
	public static final int ENCODING_MAC_ROMAN = 0;

	/**
	 * Windows encodings id
	 */
	public static final int ENCODING_WIN_SYMBOL = 0;
	public static final int ENCODING_WIN_UNICODE_BMP = 1;
	public static final int ENCODING_WIN_SHIFT_JIS = 2;
	public static final int ENCODING_WIN_BIG5 = 3;
	public static final int ENCODING_WIN_PRC = 4;
	public static final int ENCODING_WIN_WANSUNG = 5;
	public static final int ENCODING_WIN_JOHAB = 6;
	public static final int ENCODING_WIN_UNICODE_FULL = 10;

	/**
	 * Unicode encodings
	 */
	public static final int ENCODING_UNICODE_1_0 = 0;
	public static final int ENCODING_UNICODE_1_1 = 1;
	public static final int ENCODING_UNICODE_2_0_BMP = 3;
	public static final int ENCODING_UNICODE_2_0_FULL = 4;

	/**
	 * cmap变名称
	 */
	public static final String TAG = "cmap";

	private CmapSubtable[] cmapSubtable;

	public CmapTable(WoffFont woffFont) {
		super(woffFont);
	}

	@Override
	public void read(WoffFont woffFont, AbstractOTFDataStream stream) throws IOException {
		long currentPosition = stream.getCurrentPosition();
		stream.seek(offset);
		byte[] fontData = stream.read(compLength);
		byte[] inflateFont = inflateFontData(fontData);
		AbstractOTFDataStream data = new ByteBufferOTFDataStream(inflateFont);
		int version = data.readUnsignedShort();
		int numTables = data.readUnsignedShort();

		cmapSubtable = new CmapSubtable[numTables];
		for (int i = 0; i < numTables; i++) {
			CmapSubtable table = new CmapSubtable();
			table.initData(data);

			cmapSubtable[i] = table;
		}
		for (int i = 0; i < numTables; i++) {
			cmapSubtable[i].initSubtable(this, woffFont.getNumberOfGlyphs(), data);
		}
	}

	public CmapSubtable[] getCmapSubtable() {
		return cmapSubtable;
	}

	public void setCmapSubtable(CmapSubtable[] cmapSubtable) {
		this.cmapSubtable = cmapSubtable;
	}

	public CmapSubtable getSubtable(int platformId, int platformEncodingId) {
		for (CmapSubtable cmap : cmapSubtable) {
			if (cmap.getPlatformId() == platformId && cmap.getPlatformId() == platformEncodingId) {
				return cmap;
			}
		}
		return null;
	}

	/**
	 * 获取windows平台上的cmap表和字形映射
	 */
	public Map<Integer, Integer> getWindowCharacterCodeToGlyphId() {
		for (CmapSubtable cmap : cmapSubtable) {
			if (cmap.getPlatformId() == PLATFORM_WINDOWS) {
				return cmap.getCharacterCodeToGlyphId();
			}
		}
		return null;
	}
	
	@Override
	public String toString() {
		return "AbstractWoffTable{" + "tag='" + tag + '\'' + ", offset=" + offset + ", compLength=" + compLength
				+ ", origLength=" + origLength + ", origChecksum=" + origChecksum + ", cmapSubtable="
				+ Arrays.toString(cmapSubtable) + '}';
	}
}
